import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/stock_picking_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/stock_picking.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import '../../../generated/l10n.dart';

class StockPickingEdit extends StatefulWidget {
  StockPickingEdit({this.stockPicking});

  final StockPicking? stockPicking;

  @override
  _StockPickingEditState createState() => _StockPickingEditState();
}

class _StockPickingEditState extends State<StockPickingEdit> {
  late StockPicking stockPicking;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  //late List<SelectOptionVO> activeList;

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.stockPicking == null;
    this.stockPicking = widget.stockPicking ?? StockPicking();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    //activeList=[SelectOptionVO(value:'0',label: "禁用"),SelectOptionVO(value:'1',label: "启用")];
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '出入库单号',
            value: stockPicking.name,
            onSaved: (v) {
              stockPicking.name = v;
            },
          ),
          CryInput(
            label: '源库房',
            value: stockPicking.locationId,
            onSaved: (v) {
              stockPicking.locationId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '目标库房',
            value: stockPicking.locationDestId,
            onSaved: (v) {
              stockPicking.locationDestId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '出入库类型',
            value: stockPicking.pickingTypeId,
            onSaved: (v) {
              stockPicking.pickingTypeId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: 'create_uid',
            value: stockPicking.createUid,
            onSaved: (v) {
              stockPicking.createUid = v;
            },
          ),
          CrySelectDate(
            context,
            label: 'create_date',
            value: stockPicking.createDate,
            onSaved: (v) {
              stockPicking.createDate = v;
            },
          ),
          CryInput(
            label: 'write_uid',
            value: stockPicking.writeUid,
            onSaved: (v) {
              stockPicking.writeUid = v;
            },
          ),
          CrySelectDate(
            context,
            label: 'write_date',
            value: stockPicking.writeDate,
            onSaved: (v) {
              stockPicking.writeDate = v;
            },
          ),
          CryInput(
            label: '生产单号',
            value: stockPicking.productorderno,
            onSaved: (v) {
              stockPicking.productorderno = v;
            },
          ),
          CryInput(
            label: '批次号',
            value: stockPicking.batchno,
            onSaved: (v) {
              stockPicking.batchno = v;
            },
          ),
          CryInput(
            label: '工序',
            value: stockPicking.processId,
            onSaved: (v) {
              stockPicking.processId = v;
            },
          ),
          CryInput(
            label: '设备工位',
            value: stockPicking.stationId,
            onSaved: (v) {
              stockPicking.stationId = v;
            },
          ),
          CryInput(
            label: '作业员',
            value: stockPicking.instockuserId,
            onSaved: (v) {
              stockPicking.instockuserId = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            StockPickingOdooApi.saveOrUpdate(stockPicking.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
